//
//  MovieTableViewController.m
//  MansApp
//
//  Created by lanou3g on 15/7/13.
//  Copyright (c) 2015年 ACH. All rights reserved.
//

#import "MovieTableViewController.h"
#import "MovieTableViewCell.h"
#import "MovieListModel.h"
#import "MovieDetailViewController.h"
@interface MovieTableViewController ()
@property (nonatomic,strong) NSMutableArray *movieArray;
@property (nonatomic,strong) NSMutableArray *gameArray;
@property (nonatomic,strong) NSMutableArray *noboredArray;
@property (nonatomic,strong) NSString *idString;
@end

@implementation MovieTableViewController
- (NSMutableArray *)movieArray
{
    if (!_movieArray) {
        _movieArray = [[NSMutableArray alloc]init];
    }
    return _movieArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self threeForTag];
    
    [self.tableView registerClass:[MovieTableViewCell class] forCellReuseIdentifier:@"movieClass"];
    
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerAction)];
    self.tableView.header = header;
    
    MJRefreshAutoGifFooter *footer = [MJRefreshAutoGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerAction)];
    self.tableView.footer = footer;
}
//根据tag判断解析方法
- (void)threeForTag
{
    if (self.tag == 100) {
        self.navigationItem.title = @"电影志";
        [self jiexiMovieList:kMovieList];
    }
    if (self.tag == 200) {
        self.navigationItem.title = @"游戏世界";
        [self jiexiMovieList:kGameListUrl];
    }
    if (self.tag == 300) {
        self.navigationItem.title = @"不许无聊";
        [self jiexiMovieList:kNoboredUrl];
    }
}

//下拉刷新
- (void)headerAction
{
    [self threeForTag];
    [self.tableView.header endRefreshing];
}
//上拉加载
- (void)footerAction
{
    MovieListModel *model = [_movieArray lastObject];
    NSString *str = model.movieID;
    if (self.tag == 100) {
        NSString *movieUrl = [NSString stringWithFormat:@"%@%@",kNextMovieUrl,str];
        [self jiexiMovieList:movieUrl];
    }
    if (self.tag == 200) {
        NSString *movieUrl = [NSString stringWithFormat:@"%@%@",kNectGameUrl,str];
        [self jiexiMovieList:movieUrl];
    }
    if (self.tag == 300) {
        NSString *movieUrl = [NSString stringWithFormat:@"%@%@",kNextBoredUrl,str];
        [self jiexiMovieList:movieUrl];
    }
    [self.tableView.footer endRefreshing];
    
}

- (void)jiexiMovieList:(NSString *)url
{
    [ACHNetWork GET:url success:^(id response) {
        NSArray *arr = [response objectForKey:@"stories"];
        NSMutableArray *allArray = [NSMutableArray array];
        for (NSMutableDictionary *dic in arr) {
            MovieListModel *model = [[MovieListModel alloc]init];
            model.title = [dic objectForKey:@"title"];
            model.movieID = [dic objectForKey:@"id"];
            NSArray *array = [dic objectForKey:@"images"];
            model.images = [array objectAtIndex:0];
            
            [allArray addObject:model];
        }
        [self.movieArray addObjectsFromArray:allArray];
        
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return _movieArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MovieTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"movieClass" forIndexPath:indexPath];
    MovieListModel *model = _movieArray[indexPath.row];
    NSURL *url = [NSURL URLWithString:model.images];
    [cell.moviewPicImageView sd_setImageWithURL:url];
    
    if (model.images == nil) {
        CGRect frame = CGRectMake(kP6Height(10), kP6Height(10), kWidth - kP6Height(20) ,kP6Height(60));
        cell.movieTitleLabel.frame = frame;
    }
    else
    {
        cell.movieTitleLabel.frame = CGRectMake(kP6Height(10), kP6Height(10), kWidth - cell.moviewPicImageView.frame.size.width - kP6Height(30),kP6Height(60));
    }
    
    cell.movieTitleLabel.text = model.title;
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MovieDetailViewController *movieDetailVC = [[MovieDetailViewController alloc]init];
    MovieListModel *model = _movieArray[indexPath.row];
    movieDetailVC.idString = model.movieID;
    movieDetailVC.titleString = model.title;
    movieDetailVC.temp = self.tag;
    [self.navigationController pushViewController:movieDetailVC animated:NO];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kP6Height(80);
}

/*`1`
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
